import logging
import random
import re
from datetime import datetime

from info.libs.yuntongxun.sms import CCP
from info.models import User
from info.utils.response_code import RET
from . import passport_blue
from flask import render_template, abort, make_response, jsonify
from flask import current_app
from info import redis_store, constants, db
from flask import request
from info.utils.captcha.captcha import captcha
from flask import session

@passport_blue.route('/sms_code', methods=["POST"])
def send_message():
    params_dict = request.json
    
    mobile = params_dict.get('mobile')
    image_code = params_dict.get("image_code")
    image_code_id = params_dict.get("image_code_id")
    
    if not all([mobile, image_code_id, image_code]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数有误")
    
    if not re.match('^1[3578][0-9]{9}$', mobile):
        return jsonify(errno=RET.PARAMERR, errmsg="参数有误")
    
    try:
        real_image_code = redis_store.get("ImageCodeId_" + image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="数据查询失败")
    
    if not real_image_code:
        return jsonify(errno=RET.NODATA, errmsg="图片验证码错误")
    
    if str(real_image_code).upper() != str(image_code).upper():
        return jsonify(errno=RET.DATAERR, errmsg="验证码输入错误")
    
    sms_code_str = "%06d" % random.randint(0, 999999)
    
    # result = CCP().send_template_sms(mobile, [sms_code_str, constants.SMS_CODE_REDIS_EXPIRES // 60], "1")
    #
    # if result != 0:
    #     return jsonify(errno=RET.THIRDERR, errmsg="发送短信失败")
    print(sms_code_str)
    try:
        redis_store.set("SMS_" + mobile, sms_code_str, constants.SMS_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DATAERR, errmsg="数据保存失败")
    return jsonify(errno=RET.OK, errmsg="发送成功")


@passport_blue.route('/image_code')
def get_image_code():
    image_code_id = request.args.get('imageCodeId', None)
    
    if not image_code_id:
        return abort(403)
    
    name, text, image = captcha.generate_captcha()
    
    try:
        redis_store.set("ImageCodeId_" + image_code_id, text, constants.IMAGE_CODE_REDIS_EXPIRES)
    except Exception as e:
        logging.debug(e)
        abort(500)
    
    response = make_response(image)
    response.headers['Content-Type'] = "image/jpg"
    return image


@passport_blue.route('/register', methods=["POST"])
def register():
    params_dict = request.json
    mobile = params_dict.get('mobile') # image_code = params_dict.get("image_code")
    # image_code_id = params_dict.get("image_code_id")
    sms_code = params_dict.get('smscode')
    password = params_dict.get("password")
    
    if not all([mobile,password, sms_code]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数有误")
    
    try:
        real_sms_code = redis_store.get("SMS_" + mobile)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="短信验证失败")
    
    if not real_sms_code:
        return jsonify(errno=RET.DATAERR, errmsg="验证超时")
    
    if real_sms_code != sms_code:
        return jsonify(errno=RET.DATAERR, errmsg="验证码错误")
    
    user = User()
    user.mobile = mobile
    user.nick_name = mobile
    user.last_login = datetime.now()
    
    user.password = str(password).strip()
    
    try:
        db.session.add(user)
        db.session.commit()
    except Exception as e:
        db.session.rollback()
        return jsonify(errno=RET.DBERR)
    
    from flask import session
    session['user_id'] = user.id
    session['mobile'] = user.mobile
    session['nick_name'] = user.nick_name
    return jsonify(errno=RET.OK, errmsg="注册成功")


@passport_blue.route('/login',methods=["POST"])
def login():
    params_dict = request.json
    
    mobile = params_dict.get("mobile")
    password = params_dict.get('password')
    
    if not all([mobile, password]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数有误")
    
    if not re.match('^1[3578][0-9]{9}$', mobile):
        return jsonify(errno=RET.PARAMERR, errmsg="参数有误")
    
    try:
        user:User = User.query.filter(User.mobile == mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="数据库读取失败")
    
    if not user:
        return jsonify(errno=RET.NODATA, errmsg="用户不存在")
    
    if not user.check_password(password):
        return jsonify(errno=RET.PARAMERR, errmsg="用户名或密码错误")
    

    session['user_id'] = user.id
    session['mobile'] = user.mobile
    session['nick_name'] = user.nick_name
    
    user.last_login = datetime.now()
    try:
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()
    
    return jsonify(errno=RET.OK, errmsg="登陆成功")


@passport_blue.route('/logout',methods=["POST"])
def logout():
    from flask import session
    session.pop("user_id",None)
    session.pop("mobile",None)
    session.pop("nick_name",None)
    return  jsonify(errno=RET.OK,errmsg="退出成功")